package com.woniu.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * haha注解的自定义切面
 * @Aspect : 表示该类是一个切面类
 * @Component : 将切面类添加到容器中
 * @author tarnett
 */
@Aspect
@Component
@Slf4j
public class HaHaAspect {
    @Before("@annotation(haHa)")
    public void before(HaHa haHa){
        log.debug("进入了HaHa切面的=====前置通知=====");
    }

    @Around("@annotation(haHa)")
    public Object around(ProceedingJoinPoint joinPoint, HaHa haHa) throws Throwable {
        String name = haHa.name();
        log.debug("进入了HaHa切面的=====环绕通知=====前=====传递的参数为：{}", name);
        Object result = joinPoint.proceed();
        log.debug("进入了HaHa切面的=====环绕通知=====后=====返回值为：{}", result);
        return result;
    }

    @After("@annotation(haHa)")
    public void after(HaHa haHa){
        log.debug("进入了HaHa切面的=====后置通知=====");
    }

    @AfterThrowing(value = "@annotation(haHa)", throwing = "exception")
    public void afterThrowing(HaHa haHa, Exception exception){
        log.debug("进入了HaHa切面的=====异常通知=====抛出的异常为：{}", exception.getMessage());
    }

    @AfterReturning(value = "@annotation(haHa)", returning = "result")
    public void afterReturning(HaHa haHa, Object result){
        log.debug("进入了HaHa切面的=====返回通知=====返回结果为：{}", result);
    }
}
